package com.lantosec.hmrp.core.kit;


import com.alibaba.fastjson.JSON;
import com.lantosec.hmrp.core.properties.P;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页对象工具
 *
 * @author 杨成宇
 * @version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page<T> implements Serializable {
    private Long total = 0l; //总记录数
    private Integer page = 1;//当前页码
    private Integer totalPages = 0;//总页数
    private Integer pageSize = P.PAGE.getPageSize();//每页记录数
    private List<T> rows = new ArrayList<T>();//查询集合信息

    public Page(org.springframework.data.domain.Page p) {
        setTotal(p.getTotalElements());
        setPage(p.getNumber());
        setTotalPages(p.getTotalPages());
        setPageSize(p.getSize());
        setRows(p.getContent());
    }

    public static Page create() {
        return new Page();
    }

    public static Page create(org.springframework.data.domain.Page p) {
        return new Page(p);
    }

    public static Page by(org.springframework.data.domain.Page p) {
        return new Page(p);
    }

    public Page set(org.springframework.data.domain.Page p) {
        setTotal(p.getTotalElements());
        setPage(p.getNumber());
        setTotalPages(p.getTotalPages());
        setPageSize(p.getSize());
        setRows(p.getContent());
        return this;
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }

    public static void main(String[] args) {
        Page p = Page.create();
        System.out.println(p.toString());
        System.out.println(p.toJson());
    }
}
